package com.xiaofen.license.service;

import com.xiaofen.license.constant.LicenseClientConst;
import com.xiaofen.license.util.Base64Utils;
import com.xiaofen.license.util.FileUtils;
import com.xiaofen.license.util.RSAUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * 生成密钥密文
 *
 * @author zhang
 * @createDate: 2021/1/22 14:26
 * @version:1.0
 * @updateUser:[修改人]
 * @updateDate:[修改时间]
 * @updateRemark:[修改备注]
 */
@Component
@Configuration
@Service
public class CreateLicense {
    /**
     * 获取日志打印实例
     * */
    private Logger logger = LoggerFactory.getLogger(getClass());
    /**
     * 许可
     * */
    static String  license;

    /**
     * 公钥
     * */
    static String publicKey = LicenseClientConst.publicKey;

    /**
     * 私钥
     * */
    static String privateKey = LicenseClientConst.privateKey;

    /**
     * 项目根目录
     * */
    static String filePath = System.getProperty("user.dir");

    public String createLicenseX(String mac, String ip, String date, String type){

        return license;
    }

    /**
     * 临时数据
     * */
    private static String mac = "00-F4-8D-CB-A0-87";
    private static String ip = "192.168.1.10";
    private static String date = "20220134";
    private static String type = "0";

//    public static void main(String[] args) throws Exception{
//        CreateLicense createLicense = new CreateLicense();
//        createLicense.createLicenses();
//    }
//
//    public void createLicenses() throws Exception{
//        logger.info("公钥加密 -- -- 私钥解密");
//        String licenseData = mac + "|" + ip + "|" + date + "|" + type;
//        logger.info("加密前： \n" + licenseData);
//        byte[] data = licenseData.getBytes();
//        byte[] encodedData =  RSAUtils.encryptByPublicKey(data, publicKey);
//        String enBaseData = Base64Utils.encode(encodedData);
//        logger.info("加密后：\r\n" + enBaseData);
//
//        /**生成密钥文件*/
//        Base64Utils.byteArrayToFile(enBaseData.getBytes(), filePath + File.separator + "license.txt");
//        logger.info("license.dat：\r\n" + filePath + File.separator +"license.txt");
//
//        /**读取文件*/
//        //通过绝对路径获取文件
//        String txtData = FileUtils.readTxt(new File(filePath + File.separator + "license.txt"));
//        logger.info("=========读取 " + txtData);
//
//        /**spring boot中文件直接放在resources目录下*/
////        String s2 = FileUtils.readTxt(ResourceUtils.getFile("classpath:du.txt"));
//
//        /**解密*/
//        byte[] deBaseData = Base64Utils.decode(txtData);
//        byte[] decodedData = RSAUtils.decryptByPrivateKey(deBaseData, privateKey);
//        String target = new String(decodedData);
//        logger.info("解密后: \r\n" + target);
//
//    }

    public String createLicense() throws Exception {
        logger.info("公钥加密 -- -- 私钥解密");
        String licenseData = mac + "|" + ip + "|" + date + "|" + type;
        logger.info("加密前： \n" + licenseData);
        byte[] data = licenseData.getBytes();
        byte[] encodedData =  RSAUtils.encryptByPublicKey(data, publicKey);
        String enBaseData = Base64Utils.encode(encodedData);
        logger.info("加密后：\r\n" + enBaseData);

        /**生成密钥文件*/
        Base64Utils.byteArrayToFile(enBaseData.getBytes(), filePath + File.separator + "license.txt");
        logger.info("license.dat：\r\n" + filePath + File.separator +"license.txt");

        /**读取文件*/
        //通过绝对路径获取文件
        String txtData = FileUtils.readTxt(new File(filePath + File.separator + "license.txt"));

        /**spring boot中文件直接放在resources目录下*/
//        String s2 = FileUtils.readTxt(ResourceUtils.getFile("classpath:du.txt"));

        /**解密*/
        byte[] deBaseData = Base64Utils.decode(txtData);
        byte[] decodedData = RSAUtils.decryptByPrivateKey(deBaseData, privateKey);
        String target = new String(decodedData);
        logger.info("解密后: \r\n" + target);

        return enBaseData;
    }
}
